package com.we.connect.weconnect.utils;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.lang.management.ManagementFactory;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 时间工具类
 *
 */
public class DateUtils extends org.apache.commons.lang3.time.DateUtils {
    public static String YYYY = "yyyy";

    public static String YYYY_MM = "yyyy-MM";

    public static String YYYY_MM_DD = "yyyy-MM-dd";

    public static String YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

    public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss";
    public static String YYYY_MM_DD_HH_MM = "yyyy-MM-dd HH:mm";

    private static String[] parsePatterns =
            {"yyyy-MM-dd", "yyyy-MM-dd HH:mm:ss", "yyyy-MM-dd HH:mm", "yyyy-MM", "yyyy/MM/dd", "yyyy/MM/dd HH:mm:ss",
                    "yyyy/MM/dd HH:mm", "yyyy/MM", "yyyy.MM.dd", "yyyy.MM.dd HH:mm:ss", "yyyy.MM.dd HH:mm", "yyyy.MM"};

    /**
     * 获取当前Date型日期
     *
     * @return Date() 当前日期
     */
    public static Date getNowDate() {
        return new Date();
    }

    /**
     * 获取当前日期, 默认格式为yyyy-MM-dd
     *
     * @return String
     */
    public static String getDate() {
        return dateTimeNow(YYYY_MM_DD);
    }

    public static final String getTime() {
        return dateTimeNow(YYYY_MM_DD_HH_MM_SS);
    }

    public static final String dateTimeNow() {
        return dateTimeNow(YYYYMMDDHHMMSS);
    }

    public static final String dateTimeNow(final String format) {
        return parseDateToStr(format, new Date());
    }

    public static final String dateTime(final Date date) {
        return parseDateToStr(YYYY_MM_DD, date);
    }

    public static final String parseDateToStr(final String format, final Date date) {
        return new SimpleDateFormat(format).format(date);
    }

    public static final Date dateTime(final String format, final String ts) {
        try {
            return new SimpleDateFormat(format).parse(ts);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 日期路径 即年/月/日 如2018/08/08
     */
    public static final String datePath() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy/MM/dd");
    }

    /**
     * 日期路径 即年/月/日 如2018年08月08日
     */
    public static final String datePathChina() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyy年MM月dd日");
    }

    /**
     * 日期路径 即年/月/日 如20180808
     */
    public static final String dateTime() {
        Date now = new Date();
        return DateFormatUtils.format(now, "yyyyMMdd");
    }

    public static final String dateMonth() {
        Date now = new Date();
        return DateFormatUtils.format(now, YYYY_MM);
    }

    public static String dateStr(String dateStr) {
        if(StringUtils.isEmpty(dateStr)){
            return null;
        }
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);

        try {
            Date date = format.parse(dateStr);
            return DateFormatUtils.format(date, YYYY_MM_DD);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取上一个月1号的时间
     */
    public static String getBeforeMonthFirstDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(calendar.getTime());
    }

    /**
     * 获取上个月的最后一天的时间
     */
    public static String getBeforeMonthLastDate() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return sf.format(calendar.getTime());
    }

    /**
     * 获取这个月1号的时间
     */
    public static String getMonthFirstDate() {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(calendar.getTime());
    }

    /**
     * 获取这个月的最后一天的时间
     */
    public static String getMonthLastDate() {
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        int month = calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month - 1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return sf.format(calendar.getTime());
    }

    /**
     * 日期型字符串转化为日期 格式
     */
    public static Date parseDate(Object str) {
        if (str == null) {
            return null;
        }
        try {
            return parseDate(str.toString(), parsePatterns);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 获取服务器启动时间
     */
    public static Date getServerStartDate() {
        long time = ManagementFactory.getRuntimeMXBean().getStartTime();
        return new Date(time);
    }

    public static String getDatePoor(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        return day + "天" + hour + "小时" + min + "分钟";
    }

    /**
     * 计算两个时间差
     */
    public static Long getDateGap(Date endDate, Date nowDate) {
        long nd = 1000 * 24 * 60 * 60;
        long nh = 1000 * 60 * 60;
        long nm = 1000 * 60;
        // long ns = 1000;
        // 获得两个时间的毫秒时间差异
        long diff = endDate.getTime() - nowDate.getTime();
        // 计算差多少天
        long day = diff / nd;
        // 计算差多少小时
        long hour = diff % nd / nh;
        // 计算差多少分钟
        long min = diff % nd % nh / nm;
        // 计算差多少秒//输出结果
        // long sec = diff % nd % nh % nm / ns;
        // return day + "天" + hour + "小时" + min + "分钟";
        return day;
    }

    /**
     * 计算两个时间差
     */
    public static Long getDateGap(String startDateStr, String endDateStr) {
        long nd = 1000 * 24 * 60 * 60;
        try {
            Date startDate = parseDate(startDateStr, YYYY_MM_DD);
            Date endDate = parseDate(endDateStr, YYYY_MM_DD);
            long diff = endDate.getTime() - startDate.getTime();
            // 计算差多少天
            long day = diff / nd;
            return day;
        } catch (ParseException e) {
            return null;
        }

    }

    /**
     * 计算两个时间差
     */
    public static Long getDateGapForNow(String startDateStr) {
        if (StringUtils.isEmpty(startDateStr)) {
            return null;
        }
        long nd = 1000 * 24 * 60 * 60;
        try {
            Date startDate = parseDate(startDateStr, YYYY_MM_DD);
            Date endDate = new Date();
            long diff = endDate.getTime() - startDate.getTime();
            // 计算差多少天
            long day = diff / nd;
            return day;
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * 计算两个时间差
     */
    public static Long getDateTimeGapForNow(String startTimeStr) {
        if (StringUtils.isEmpty(startTimeStr)) {
            return null;
        }
        long nd = 1000 * 24 * 60 * 60;
        try {
            Date startDate = parseDate(startTimeStr, YYYY_MM_DD_HH_MM_SS);
            Date endDate = new Date();
            long diff = endDate.getTime() - startDate.getTime();
            // 计算差多少天
            long day = diff / nd;
            return day;
        } catch (Exception e) {
            return null;
        }
    }

    public static Long getDayGapForNow(String startTimeStr) {
        if (StringUtils.isEmpty(startTimeStr)) {
            return null;
        }
        long nd = 1000 * 24 * 60 * 60;
        try {
            Date startDate = parseDate(startTimeStr, YYYY_MM_DD);
            Date endDate = new Date();
            long diff = endDate.getTime() - startDate.getTime();
            // 计算差多少天
            long day = diff / nd;
            return day;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取本月的第一天
     *
     * @return
     */
    public static String getMonthFirstDay() {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        Calendar calstr = Calendar.getInstance();
        // 本月
        calstr.add(Calendar.MONTH, 0);
        // 设置为1号为本月第一天
        calstr.set(Calendar.DAY_OF_MONTH, 1);
        return format.format(calstr.getTime());
    }

    public static long getCurrentTimeMillis() {
        return System.currentTimeMillis();
    }

    /**
     * 格式化date
     */
    public static String getDateTimeStr(Date date) {
        SimpleDateFormat format = new SimpleDateFormat(YYYY_MM_DD_HH_MM_SS);
        if (date == null) {
            return "";
        }
        return format.format(date);
    }

    /**
     * 获取时间格式化后的字符串
     */
    public static String timeCycle(long sSeconds) {
        StringBuilder time = new StringBuilder();
        long seconds = sSeconds % 60;
        long minutes = (sSeconds / 60) % 60;
        long hours = (sSeconds / 3600) ;
//        long hours = (sSeconds / 3600) % 24;
//        long days = sSeconds / 3600 / 24;
//        if (days > 0) {
//            time.append(days).append("天");
//        }
        time.append(hours).append("小时").append(minutes).append("分钟").append(seconds).append("秒");
        return time.toString();
    }

    public static Date transToDate(String s) {
        Date date = new Date();
        try {
            date = new SimpleDateFormat(YYYY_MM_DD_HH_MM).parse(s);
        } catch (ParseException e) {
            return null;
        }
        return date;
    }

    public static String getDateDifferences(String start, String end){
        DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
        try {
            Date star = dft.parse(start);//开始时间
            Date endDay=dft.parse(end);//结束时间
            Long starTime=star.getTime();
            Long endTime=endDay.getTime();
            Long num=endTime-starTime;//时间戳相差的毫秒数
            return String.valueOf(num/24/60/60/1000);//除以一天的毫秒数
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }
}
